const db = require("../utils/db");

// 病例-获取病例列表
exports.caseListGet = async (params) => {
    return new Promise(((resolve, reject) => {
        let sql = '';
        if (params.c_patient && params.c_department) {
            sql = `select * from caselist,doctor,department,patient where patient.p_name like '%${params.c_patient}%' and department.d_name like '%${params.c_department}%'` + ' '  + `and doctor.doc_id = caselist.c_doctor and department.d_id = caselist.c_department and patient.p_id = caselist.c_patient and caselist.c_hospital = ${params.hospitalId}`
        } else if (params.c_patient) {
            sql = `select * from caselist,doctor,department,patient where patient.p_name like '%${params.c_patient}%'`+ ' ' + `and doctor.doc_id=caselist.c_doctor and department.d_id = caselist.c_department and patient.p_id = caselist.c_patient and caselist.c_hospital = ${params.hospitalId}`
        } else if (params.c_department) {
            sql = `select * from caselist,doctor,department,patient where department.d_name like '%${params.c_department}%'`+ ' ' + 'and doctor.doc_id=caselist.c_doctor and department.d_id = caselist.c_department and patient.p_id = caselist.c_patient'
        }else if (params.c_id) {
            sql = `select * from caselist,doctor,department,patient where c_id like '%${params.c_id}%'` + ' ' +`and doctor.doc_id=caselist.c_doctor and department.d_id = caselist.c_department and patient.p_id = caselist.c_patient and caselist.c_hospital = ${params.hospitalId}`
        } else {
            sql = `select * from caselist,doctor,department,patient where doctor.doc_id=caselist.c_doctor and department.d_id = caselist.c_department and patient.p_id = caselist.c_patient and caselist.c_hospital = ${params.hospitalId}`
        }
        db.query(sql).then(res => {
            console.log(res, "caseListGet sql查询成功结果")
            let result = {};
            result.list = res.slice((params.pageIndex - 1) * (params.pageSize), (params.pageIndex - 1) * (params.pageSize) + params.pageSize);
            result.total = res.length;
            result.pageIndex = params.pageIndex;
            result.pageSize = params.pageSize;
            resolve(result);
        }).catch(error => {
            console.log(error, "caseListGet sql查询失败结果")
            reject(error)

        })
    }))
}